package com.example.smartrefreshlayoutdemo.activity.practice;

import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.smartrefreshlayoutdemo.R;
import com.example.smartrefreshlayoutdemo.adapter.BaseRecyclerAdapter;
import com.example.smartrefreshlayoutdemo.adapter.SmartViewHolder;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.scwang.smart.refresh.layout.util.SmartUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class RepastPracticeActivity extends AppCompatActivity {
    private class Model{
        int imageId;
        int avatarId;
        String name;
        String nickname;
    }
    private static boolean isFirstEnter = true;
    private RepastQuickAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_practice_repast);

        //点击工具栏箭头退出activity
        final Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        final RefreshLayout refreshLayout = findViewById(R.id.refreshLayout);
        //设置是否在没有更多数据之后 Footer 跟随内容
        refreshLayout.setEnableFooterFollowWhenNoMoreData(true);

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(mAdapter = new RepastQuickAdapter(R.layout.item_practice_repast,loadModels()));

        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (mAdapter.getData().size() > 12) {
                            Toast.makeText(getBaseContext(), "数据全部加载完毕", Toast.LENGTH_SHORT).show();
                            refreshLayout.finishLoadMoreWithNoMoreData();
                        }else{
                            mAdapter.addData(loadModels());
                            refreshLayout.finishLoadMore();
                        }
                    }
                }, 1000);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshLayout.finishRefresh();
                        refreshLayout.resetNoMoreData();
                    }
                }, 2000);
            }
        });
        refreshLayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setHeaderInsetStart(SmartUtil.px2dp(toolbar.getHeight()));
            }
        }, 500);

        /*StatusBarUtil.darkMode(this);
        StatusBarUtil.setPaddingSmart(this,recyclerView);
        StatusBarUtil.setPaddingSmart(this,toolbar);
        StatusBarUtil.setPaddingSmart(this,findViewById(R.id.blurView));*/
    }

    /**
     * 模拟数据
     */
    private List<Model> loadModels() {
        List<Model> models = new ArrayList<>();
        models.add(new Model() {{
            this.name = "但家香酥鸭";
            this.nickname = "爱过那张脸";
            this.imageId = R.mipmap.image_practice_repast_1;
            this.avatarId = R.mipmap.image_avatar_1;
        }});
        models.add(new Model() {{
            this.name = "香菇蒸鸟蛋";
            this.nickname = "淑女算个鸟";
            this.imageId = R.mipmap.image_practice_repast_2;
            this.avatarId = R.mipmap.image_avatar_2;
        }});
        models.add(new Model() {{
            this.name = "花溪牛肉粉";
            this.nickname = "性感妩媚";
            this.imageId = R.mipmap.image_practice_repast_3;
            this.avatarId = R.mipmap.image_avatar_3;
        }});
        models.add(new Model() {{
            this.name = "破酥包";
            this.nickname = "一丝丝纯真";
            this.imageId = R.mipmap.image_practice_repast_4;
            this.avatarId = R.mipmap.image_avatar_4;
        }});
        models.add(new Model() {{
            this.name = "盐菜饭";
            this.nickname = "等着你回来";
            this.imageId = R.mipmap.image_practice_repast_5;
            this.avatarId = R.mipmap.image_avatar_5;
        }});
        models.add(new Model() {{
            this.name = "米豆腐";
            this.nickname = "宝宝树人";
            this.imageId = R.mipmap.image_practice_repast_6;
            this.avatarId = R.mipmap.image_avatar_6;
        }});
        return models;
    }

    public class RepastQuickAdapter extends BaseQuickAdapter<Model, BaseViewHolder>{

        public RepastQuickAdapter(int layoutResId, @org.jetbrains.annotations.Nullable List<Model> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(@NotNull BaseViewHolder holder, Model model) {
            holder.setImageResource(R.id.image,model.imageId);
            holder.setImageResource(R.id.avatar,model.avatarId);
            holder.setText(R.id.name,model.name);
            holder.setText(R.id.nickname,model.nickname);
        }
    }
}
